package com.sdry.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sdry.base.BaseController;
import com.sdry.constant.PartsResult;
import com.sdry.constant.PartsResultConstant;
import com.sdry.entity.PartInfo;
import com.sdry.entity.PartInfoExample;
import com.sdry.entity.PartOrder;
import com.sdry.entity.PartOrderDetail;
import com.sdry.entity.PartOrderDetailExample;
import com.sdry.entity.PartOrderDetailExample.Criteria;
import com.sdry.entity.PartOrderExample;
import com.sdry.service.PartInfoService;
import com.sdry.service.PartOrderDetailService;
import com.sdry.service.PartOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 采购单
 * 
 * @AUTHOR HACKER
 */
@Api(value = "采购单")
@Controller
@RequestMapping("/purchase/order")
public class PartOrderController extends BaseController {

	@Autowired
	private PartOrderService partOrderService;

	@Autowired
	private PartOrderDetailService partOrderDetailService;

	@Autowired
	private PartInfoService PartInfoService;

	/**
	 * 首页
	 * @return
	 */
	@ApiOperation(value = "首页")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		return "/purchase/order/index";
	}

	/**
	 * 列表
	 * @param offset
	 * @param limit
	 * @param search
	 * @param sort
	 * @param order
	 * @return
	 */
	@ApiOperation(value = "列表")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Object list(@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
			@RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
			@RequestParam(required = false, defaultValue = "", value = "search") String search,
			@RequestParam(required = false, value = "sort") String sort,
			@RequestParam(required = false, value = "order") String order) {
		PartOrderExample partOrderExample = new PartOrderExample();
		if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
			partOrderExample.setOrderByClause(sort + " " + order);
		}
		if (StringUtils.isNotBlank(search)) {
			partOrderExample.or().andNameLike("%" + search + "%");
		}
		List<PartOrder> rows = partOrderService.selectByExampleForOffsetPage(partOrderExample, offset, limit);
		long total = partOrderService.countByExample(partOrderExample);
		Map<String, Object> result = new HashMap<>();
		result.put("rows", rows);
		result.put("total", total);
		return result;
	}

	/**
	 * 新增
	 * @param modelMap
	 * @return
	 */
	@ApiOperation(value = "新增")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(ModelMap modelMap) {
		PartInfoExample example = new PartInfoExample();
		List<PartInfo> partInfos = PartInfoService.selectByExample(example);
		modelMap.put("partInfos", partInfos);
		return "/purchase/order/create";
	}

	/**
	 * 新增
	 * @param partOrder
	 * @param partOrderDetail
	 * @return
	 */
	@ApiOperation(value = "新增")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Object create(PartOrder partOrder, PartOrderDetail partOrderDetail) {
		int conut1 = partOrderService.insert(partOrder);

		PartInfo partInfo = PartInfoService.selectByPrimaryKey(partOrderDetail.getInfoId());
		partOrderDetail.setInfoName(partInfo.getName());
		partOrderDetail.setInfoModel(partInfo.getModel());
		partOrderDetail.setInfoId(partInfo.getInfoId());
		partOrderDetail.setOrderId(partOrder.getOrderId());
		int count2 = partOrderDetailService.insert(partOrderDetail);
		if (conut1 == 0 || count2 == 0) {
			return new PartsResult(PartsResultConstant.FAILED, "");
		}
		return new PartsResult(PartsResultConstant.SUCCESS, 1);
	}

	/**
	 * 修改
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@ApiOperation(value = "修改")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable("id") Long id, ModelMap modelMap) {
		PartOrder partOrder = partOrderService.selectByPrimaryKey(id);
		
		PartInfoExample example = new PartInfoExample();
		List<PartInfo> partInfos = PartInfoService.selectByExample(example);

		PartOrderDetailExample partOrderDetailExample = new PartOrderDetailExample();
		Criteria criteria = partOrderDetailExample.createCriteria();
		criteria.andOrderIdEqualTo(id);
		List<PartOrderDetail> partOrderDetails = partOrderDetailService.selectByExample(partOrderDetailExample);

		modelMap.put("partInfos", partInfos);
		modelMap.put("partOrder", partOrder);
		modelMap.put("partOrderDetail", partOrderDetails.get(0));

		return "/purchase/order/update";
	}

	/**
	 * 修改
	 * @param id
	 * @param partOrder
	 * @param partOrderDetail
	 * @return
	 */
	@ApiOperation(value = "修改")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	@ResponseBody
	public Object update(@PathVariable("id") int id, PartOrder partOrder, PartOrderDetail partOrderDetail) {
		int conut1 = partOrderService.updateByPrimaryKeySelective(partOrder);

		PartInfo partInfo = PartInfoService.selectByPrimaryKey(partOrderDetail.getInfoId());
		partOrderDetail.setInfoName(partInfo.getName());
		partOrderDetail.setInfoModel(partInfo.getModel());
		partOrderDetail.setInfoId(partInfo.getInfoId());
		partOrderDetail.setOrderId(partOrder.getOrderId());
		int count2 = partOrderDetailService.updateByPrimaryKeySelective(partOrderDetail);

		if (conut1 == 0 && count2 == 0) {
			return new PartsResult(PartsResultConstant.FAILED, "");
		}
		return new PartsResult(PartsResultConstant.SUCCESS, 1);
	}

	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@ApiOperation(value = "删除")
	@RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
	@ResponseBody
	public Object delete(@PathVariable("ids") String ids) {
		int count = partOrderService.deleteByPrimaryKeys(ids);

		String[] idArray = ids.split("-");
		for (String idStr : idArray) {
			if (StringUtils.isBlank(idStr)) {
				continue;
			}
			Long id = Long.parseLong(idStr);
			PartOrderDetailExample example = new PartOrderDetailExample();
			Criteria criteria = example.createCriteria();
			criteria.andOrderIdEqualTo(id);
			partOrderDetailService.deleteByExample(example);
		}
		if (count == 0) {
			return new PartsResult(PartsResultConstant.FAILED, "");
		}
		return new PartsResult(PartsResultConstant.SUCCESS, 1);
	}

}
